package com.platform.api;

import com.platform.annotation.LoginUser;
import com.platform.entity.ApiAmountBillVo;
import com.platform.entity.ApiWithdrawBillVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAmountBillService;
import com.platform.service.ApiUserService;
import com.platform.service.ApiWithdrawBillService;
import com.platform.util.UtilServiceName;
import com.platform.utils.IdUtil;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 窝币账单流水
 *
 * @author zhurusen
 * @email 524227263@qq.com
 */
@RestController
@RequestMapping("/api/withdraw")
public class ApiWithdrawBillController {

    @Autowired
    private ApiWithdrawBillService apiWithdrawBillService;
    @Autowired
    private ApiAmountBillService apiAmountBillService;
    @Autowired
    private ApiUserService apiUserService;

    /**
     * 提现申请
     *
     * @param loginUser
     * @param alipayNo   支付宝账号
     * @param alipayName 支付宝用户名
     * @param phone
     * @param amount     提现金额
     * @return
     */
    @RequestMapping("add")
    public R withdraw(@LoginUser UserVo loginUser, String alipayNo, String phone, String alipayName, BigDecimal amount) {

        if (amount.compareTo(loginUser.getAvailable_amount()) > 0) {
            return R.ok(1, "超出可提现余额", "");
        }


        ApiWithdrawBillVo info = new ApiWithdrawBillVo();
        info.setCreateDate(new Date());
        info.setUserId(loginUser.getId());
        info.setPhone(phone);
        info.setAlipayNo(alipayNo);
        info.setAlipayName(alipayName);
        info.setAmount(amount);
        info.setSerialNumber(IdUtil.createIdbyUUID());
        apiWithdrawBillService.save(info);
        apiUserService.recharge(loginUser, "del", amount);


        UserVo userVo = apiUserService.queryObject(loginUser.getId());

        Map<String, Object> map = new HashMap<>();

        return R.ok(0, "提交成功，等待审核", userVo.getAvailable_amount());
    }

    /**
     * 提现申请
     *
     * @param loginUser
     * @param alipayNo   支付宝账号
     * @param alipayName 支付宝用户名
     * @param phone
     * @param amount     提现金额
     * @return
     */
    @RequestMapping("add_v2")
    public R withdrawV2(@LoginUser UserVo loginUser, String alipayNo, String phone, String alipayName, BigDecimal amount) {

        if (amount.compareTo(loginUser.getPresent_amount()) > 0) {
            return R.ok(1, "超出可提现余额", "");
        }

        if (amount.compareTo(new BigDecimal(100)) < 0) {
            return R.ok(1, "最低提现金额为100元", "");
        }

        ApiWithdrawBillVo info = new ApiWithdrawBillVo();
        info.setCreateDate(new Date());
        info.setUserId(loginUser.getId());
        info.setPhone(phone);
        info.setStatus("0");
        info.setAlipayNo(alipayNo);
        info.setAlipayName(alipayName);
        info.setAmount(amount);
        info.setSerialNumber(IdUtil.createIdbyUUID());
        apiWithdrawBillService.save(info);
        apiUserService.recharge(loginUser, "del", amount);


        UserVo userVo = apiUserService.queryObject(loginUser.getId());

        Map<String, Object> map = new HashMap<>();

        map.put("availableAmount", userVo.getAvailable_amount());
        map.put("presentAmount", userVo.getPresent_amount());

        /**
         * 支出
         */
        ApiAmountBillVo billVo = new ApiAmountBillVo();
        billVo.setBillNo(IdUtil.createIdbyUUID());
        billVo.setCreateTime(new Date());
        billVo.setType(1);
        billVo.setMouldId(info.getId());
        billVo.setMouldName("withdraw");
        billVo.setTitle(UtilServiceName.serviceNmae("withdraw"));
        billVo.setUserId(loginUser.getId());
        billVo.setAmount(amount);
        apiAmountBillService.save(billVo);

        return R.ok(0, "提交成功，等待审核", map);
    }

    /**
     * 提现明细
     *
     * @param loginUser
     * @param page
     * @return
     */
    @RequestMapping("list")
    public R list(@LoginUser UserVo loginUser, Integer status, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);

        params.put("sidx", "create_date");
        params.put("order", "desc");

        if (status != null) {
            params.put("status", status);
        }

        params.put("userId", loginUser.getId());

        Query query = new Query(params);


        List<ApiWithdrawBillVo> list = apiWithdrawBillService.queryList(query);

        List<Map<String, Object>> reObject = new ArrayList<>();
        for (ApiWithdrawBillVo info : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("createDate", info.getCreateDate());
            map.put("amount", info.getAmount());
            reObject.add(map);
        }

        return R.ok(0, "提交成功，等待审核", reObject);
    }


}
